import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()

// 开发环境的全局路径
// let baseUrl = "https://www.cpg2023.cn/warehouse" 
// let baseUrl = "http://localhost:9999/warehouse" 
let baseUrl = "https://youan.bobochang.work/warehouse"
// let baseUrl = "http://localhost:9999/warehouse"
let workflow = "http://CentOS的IP:9090/emos-workflow"

// 请求接口路径
Vue.prototype.url = {
	getPhoneNumber: baseUrl + "/getPhoneNumber", // 获取手机号
	login: baseUrl + "/wx-login", // 登录接口
	registration: baseUrl + "/registration", // 登录接口
	checkin: baseUrl + "/checkin/checkin", // 考勤签到
	checkinNoFace: baseUrl + "/checkin/checkin-noface", // 不用人脸签到
	createFaceModel: baseUrl + "/checkin/createFaceModel", // 创建人脸模型
	haveFace: baseUrl + "/checkin/haveFace", // 查看是否有人脸
	validCanCheckIn: baseUrl + "/checkin/validCanCheckIn", // 查询用户是否可以签到
	searchTodayCheckin: baseUrl + "/checkin/searchTodayCheckin", // 签到后返回的数据
	searchUserSummary: baseUrl + "/user/user-info", // 查询用户详细信息
	searchMonthCheckin: baseUrl + "/checkin/searchMonthCheckin", // 查询一个月内的考勤记录
	findAllRegioWork: baseUrl + "/workregion/workregion-list", // 查询全部工区
	findAllDepartment: baseUrl + "/Department/departmentId-all", // 查询全部部门
	findDepartmentById: baseUrl + "/Department/department-info", // 查询部门
	findRegioWorkById: baseUrl + "/workregion/workregion-info", // 查询工区

	updateUser: baseUrl + "/user/updateUserInfo", // 修改个人信息
	searchOneCheckin: baseUrl + "/checkin/searchOneCheckin" // 查询某天打卡信息

}

// 封装ajax，参数有路径，请求类型，数据，匿名函数（后续处理）
Vue.prototype.ajax = async function(url, method, data, fun) {
	await uni.request({
		"url": url,
		"method": method,
		"header": {
			token: uni.getStorageSync("Token")
		},
		"data": data,
		success: function(resp) {
			if (resp.data.code == 502) {
				uni.navigateTo({
					url: "/pages/login/login"
				})
			} else if (resp.statusCode == 200 && resp.data.code == 200) {
				fun(resp)
			} else if (resp.statusCode == 200 && resp.data.code != 200) {
				uni.showToast({
					icon: "none",
					title: resp.data.message
				})
				fun(resp)
			} else {
				fun(resp)
			}
		},
	})
}

Vue.prototype.checkLogin = async function() {
	return new Promise(async (resolve, reject) => {
		try {
			const token = uni.getStorageSync("Token")
			if (token) {
				await uni.request({
					"url": baseUrl + "/curr-user",
					"method": "GET",
					"header": {
						token: uni.getStorageSync("Token")
					},
					"data": null,
					success: (resp) => {
						console.log(resp)
						if (resp.data.code == 200) {
							uni.setStorageSync("userInfo", resp.data.data)
							resolve(resp.data.data)
						} else {
							uni.showToast({
								title: resp.data.message,
								mask: true,
								duration: 1000
							})
							uni.redirectTo({
								url: "../login/login?title="+resp.data.message
							})
						}
					},
					fail: (res) => {
						// uni.showToast({
						// 	title: "网络错误，请重新登录",
						// 	mask: true,
						// 	duration: 1000
						// })
						uni.redirectTo({
							url: "../login/login?title=请求错误，请重新登录"
						})
					}
				})
			} else {
				uni.redirectTo({
					url: "../login/login?title=请先登录"
				})
			}
		} catch {
			uni.redirectTo({
				url: "../login/login?title=系统错误"
			})
		}
	})
}